package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.datacenter.entity.DcCompoundTemplate;
import com.thinkingstar.iads.datacenter.entity.DcEquipment;

/**
 * @ClassName: DaoDcCompoundTemplate 
 * @Description:数据中心_项目_合成模板库维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcCompoundTemplate extends EntityRepositoryServiceImpl<DcCompoundTemplate>{

	public DaoDcCompoundTemplate(Dao dao) {
		super(dao);
	}
	
	public QueryResult findCompoundTemplate(NutMap param){
		List <NutMap> data_list = null;
		Pager pager = null;
		StringBuilder find_sql = new StringBuilder(" "
				+ " select dctv.* , dpdp.project_design_phase_name , dpdm.project_design_major_name"
				+ " , dpdc.project_design_content_name  "
				+ " from dc_compound_template__version dctv "
				+ " left join dc_project_design_phase dpdp on dctv.design_phase_id = dpdp.id "
				+ " left join dc_project_design_major dpdm on dctv.design_major_id = dpdm.id "
				+ " left join dc_project_design_content dpdc on dctv.design_content_id = dpdc.id "
				+ " where dctv.del_flag = '0' and dpdp.del_flag = '0' and dpdm.del_flag = '0' "
				+ " and dpdc.del_flag = '0' ");
		if(param.get("template_name") != null){
			find_sql.append(" and dctv.template_name = @template_name ");
		}
		Sql sql = Sqls.create(find_sql.toString());
		if(param.get("template_name") != null){
			sql.params().set("template_name", "%"+param.getString("template_name")+"%");
		}
		if(param.get("page_num") != null && param.get("page_size") != null){
			pager = new Pager(param.getInt("page_num"),param.getInt("page_size"));
			pager.setRecordCount(findCompoundTemplateNum(param));
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
			    List<NutMap> list = new ArrayList<>();
			    while (rs.next()){
			    	NutMap nutMap = new NutMap();
			        ResultSetMetaData metaData = rs.getMetaData();
			        int count = metaData.getColumnCount();
			        for (int i = count; i > 0; i--) {
			            String filed = metaData.getColumnName(i);
			            nutMap.put(filed, rs.getString(filed));
			        }
			        list.add(nutMap);
			    }
			    return list;
			}
		});
		dao().execute(sql);
		data_list = sql.getList(NutMap.class);
		return new QueryResult(data_list, pager);
	}
	
	public int findCompoundTemplateNum(NutMap param){
		StringBuilder find_sql = new StringBuilder(" "
				+ " select count(dctv.id) num "
				+ " from dc_compound_template__version dctv "
				+ " left join dc_project_design_phase dpdp on dctv.design_phase_id = dpdp.id "
				+ " left join dc_project_design_major dpdm on dctv.design_major_id = dpdm.id "
				+ " left join dc_project_design_content dpdc on dctv.design_content_id = dpdc.id "
				+ " where dctv.del_flag = '0' and dpdp.del_flag = '0' and dpdm.del_flag = '0' "
				+ " and dpdc.del_flag = '0' ");
		if(param.get("template_name") != null){
			find_sql.append(" and dctv.template_name = @template_name ");
		}
		Sql sql = Sqls.create(find_sql.toString());
		if(param.get("template_name") != null){
			sql.params().set("template_name", "%"+param.getString("template_name")+"%");
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				int num = 0;
				while (rs.next()) {
					num = rs.getInt("num");
				}
				return num;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	

}
